package com.usian.controller;

import com.usian.feign.ContentServiceFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backend/content")
public class ContentCategoryController {

    @Autowired
    private ContentServiceFeign contentServiceFeign;

    /**
     * @param id
     * @return
     */
    @RequestMapping("/selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0")Long id){
        List<TbContentCategory> list = contentServiceFeign.selectContentCategoryByParentId(id);
        if (list != null && list.size()>0){
            return Result.ok(list);
        }
        return Result.error("查无结果");
    }

    /**
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("/insertContentCategory")
    public Result insertContentCategory(@RequestParam Integer parentId,@RequestParam String name){
        Integer num = contentServiceFeign.insertContentCategory(parentId,name);
        if (num >0){
            return Result.ok();
        }
        return Result.error("添加失败");
    }


    /**
     * @param categoryId
     * @return
     */
    @RequestMapping("/deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam Long categoryId){
        Integer num = contentServiceFeign.deleteContentCategoryById(categoryId);
        if (num > 0){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    /**
     * 内容分类管理修改
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Long id,@RequestParam String name){
        Integer num = contentServiceFeign.updateContentCategory(id,name);
        if (num>0){
            return  Result.ok();
        }
        return Result.error("修改失败");
    }

    /**
     * @param categoryId
     * @return
     *//*
    @RequestMapping("/selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(Long categoryId){
        List<TbContent> list =contentServiceFeign.selectTbContentAllByCategoryId(categoryId);
        if (list != null && list.size()>0){
            return Result.ok(list);
        }
        return Result.error("查无结果");
    }*/

    @RequestMapping("/selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam(defaultValue = "1")Integer page,
                                                 @RequestParam(defaultValue = "30")Integer rows,
                                                 Long categoryId){
        PageResult pageResult = contentServiceFeign.selectTbContentAllByCategoryId(page,rows,categoryId);
        if (pageResult != null && pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    /**
     * @param tbContent
     * @return
     */
    @RequestMapping("/insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        Integer num = contentServiceFeign.insertTbContent(tbContent);
        if (num > 0){
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    /**
     * @param ids
     * @return
     */
    @RequestMapping("/deleteContentByIds")
    public Result deleteContentByIds(Long ids){
        Integer num = contentServiceFeign.deleteContentByIds(ids);
        if (num > 0){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
